package com.dongdong.controller;


import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.dongdong.api.RocketMQAPI;
import com.dongdong.constant.RedisUtil;
import com.dongdong.mapper.TbUserMapper;
import com.dongdong.model.TAdmin;
import com.dongdong.model.TbUser;
import com.dongdong.model.TbUserExample;
import com.dongdong.result.Result;

import com.dongdong.result.ResultEnums;
import com.dongdong.service.UserService;
import com.dongdong.utils.JwtUtil;
import com.dongdong.utils.ShiroUtils;
import com.dongdong.vo.TAdminVO;
import com.dongdong.vo.UserVO;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

    @RestController
    @RequestMapping("user")
public class UserController {

    @Autowired
    private RocketMQAPI rocketMQAPI;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserService userService;

    @Autowired
    private JwtUtil jwtUtil;



    @GetMapping("message")
    public Result message(String phone){
        rocketMQAPI.sendMessage(phone);
        return Result.OK();
    }
//注册用户
    @PostMapping("addUser")
    public Result addUser(@RequestBody UserVO userVO){
        int x = userService.addUser(userVO);
        if(x==11){
            return Result.RESPONSE(ResultEnums.MSG_ERROR); //验证码不正确
        }
        if(x==12){
            return Result.RESPONSE(ResultEnums.USERNAME_IS_OK); //用户名已经存在
        }
        return Result.OK().setData(x);
    }



    //前台用户的登录方法
    @PostMapping("/login")
    public Result login(@RequestParam String username,@RequestParam String password){
        if (StrUtil.isEmpty(username) || StrUtil.isEmpty(password)) {
            return  Result.RESPONSE(ResultEnums.USERNAME_AND_PASSWORD_NOT_NULL); //5：操作失败
        }

        //1. 获取subject
        Subject subject = SecurityUtils.getSubject();
        //2.封装Token
        UsernamePasswordToken usernamePasswordToken= new UsernamePasswordToken(username, password);
//        UserToKen userToKen= new UserToKen(username, password, UserEnum);
        //3.调用Login方法
        try {
            subject.login(usernamePasswordToken);
        }catch (UnknownAccountException e){
            e.printStackTrace();
            return  Result.RESPONSE(ResultEnums.USERNAME_AND_PASSWORD_ERROR);
        }catch(IncorrectCredentialsException e){
            return  Result.RESPONSE(ResultEnums.USERNAME_AND_PASSWORD_ERROR);
        }


        //获取user
        TbUser tbUser = (TbUser) SecurityUtils.getSubject().getPrincipal();
        //在生成token是需要做脱敏处理
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(tbUser, userVO);

        // 生成token
        String token = jwtUtil.createJwt(userVO.getId().toString(), JSONObject.toJSONString(userVO), "userRoles");

/*//         解析token   测试
         Claims claims = jwtUtil.parseJwt(token);
         String id = claims.getId();
         System.out.println("id------>>"+id);
         String userStr = claims.getSubject();
//         转换成对象  脱敏处理
        TAdminVO user1 = JSONObject.parseObject(userStr, TAdminVO.class);
         System.out.println(user1);*/

        return  Result.RESPONSE(ResultEnums.LOGIN_SUCCESS).setData(token);//登录成功

    }

    //密码找回

    //根据邮箱查询用户
    @GetMapping("/queryUserByEmail")
    public TbUser queryUserByEmail(@RequestParam String email){
        return userService.queryUserByEmail(email);
    }

     //重设密码

    @PostMapping("/resetPassword")
    public Result resetPassword(@RequestParam String username, @RequestParam String password){
        userService.resetPassword(username, password);
        return Result.OK();
    }



    @GetMapping("sendHtml")
    public String sendHtml() {
        userService.sendHtmlAndAttachmentMail();
        return "OK";
    }

    @GetMapping("sendHtmlAndFile")
    public String sendHtmlAndFile() {
        userService.sendHtmlAndAttachmentFileMail();
        return "OK";
    }



}
